package com.sfac.springboot.springboot.modules.test.controller;

import com.sfac.springboot.springboot.modules.common.vo.Result;
import com.sfac.springboot.springboot.modules.common.vo.Search;
import com.sfac.springboot.springboot.modules.test.entity.Student;
import com.sfac.springboot.springboot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {


    @Autowired
    StudentService studentService;

    /**
     * 127.0.0.1/api/jpa/student ---- post
     * {"studentName":"HymanHu"}
     * {"studentName":"HymanHu", "studentCard":{"cardNum":"20205011"}}
     */

    @PostMapping(value = "/jpa/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/jpa/student ---- put
     * {"id":"2","studentName":"HymanHu1"}
     * {"id":1,"studentName":"zyt"}
     */
    @PutMapping(value = "/jpa/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/jpa/student/1 ---- delete
     */
    @DeleteMapping("/jpa/student/{id}")
    public Result<Object> deleteStudent(int id) {
        return studentService.deleteStudent(id);
    }

    /**
     * 127.0.0.1/api/jpa/student/1 ---- get
     */
    @GetMapping("/jpa/student/{id}")
    public Student getStudentById(int id) {
        return studentService.getStudentById(id);
    }

    /**
     * 127.0.0.1/api/students ---- post
     * {"currentPage":1,"pageSize":5,"sort":"studentName","direction":"desc","keyword":""}
     */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page getStudentsBySearch(@RequestBody Search search) {
        return studentService.getStudentsBySearch(search);
    }

    /*
    * 127.0.0.1/api/students?studentName=hyh1   ------get
    * */
    @GetMapping("/students")
    public List<Student> getStudentByStudentName(@RequestBody String studentName) {
        return studentService.getStudentByStudentName(studentName);
    }
}